1 using System;
2 using UnityEngine;
3
4 namespace UnityStandardAssets.ImageEffects
5 {
6 [ExecuteInEditMode]
7 [RequireComponent(typeof(Camera))]
8 [AddComponentMenu("Image Effects/Color Adjustments/Contrast Enhance (Unsharp Mask)")]
9 public class ContrastEnhance : PostEffectsBase
10 {
11 [Range(0.0f, 1.0f)]
12 public float intensity = 0.5f;
13 [Range(0.0f,0.999f)]
14 public float threshold = 0.0f;
15
16 private Material separableBlurMaterial;
17 private Material contrastCompositeMaterial;
18
19 [Range(0.0f,1.0f)]
20 public float blurSpread = 1.0f;
21
22 public Shader separableBlurShader = null;
23 public Shader contrastCompositeShader = null;
24
25
26 public override bool CheckResources ()
27 {
28 CheckSupport (false);
29
30 contrastCompositeMaterial = CheckShaderAndCreateMaterial (contrastCompositeShader, contrastCompositeMaterial);
31 separableBlurMaterial = CheckShaderAndCreateMaterial (separableBlurShader, separableBlurMaterial);
32
33 if (!isSupported)
34 ReportAutoDisable ();
35 return isSupported;
36 }
37
38 void OnRenderImage (RenderTexture source, RenderTexture destination)
39 {
40 if (CheckResources()==false)
41 {
42 Graphics.Blit (source, destination);
43 return;
44 }
45
46 int rtW = source.width;
47 int rtH = source.height;
48
49 RenderTexture color2 = RenderTexture.GetTemporary (rtW/2, rtH/2, 0);
50
51 // downsample
52
53 Graphics.Blit (source, color2);
54 RenderTexture color4a = RenderTexture.GetTemporary (rtW/4, rtH/4, 0);
55 Graphics.Blit (color2, color4a);
56 RenderTexture.ReleaseTemporary (color2);
57
58 // blur
59
60 separableBlurMaterial.SetVector ("offsets", new Vector4 (0.0f, (blurSpread * 1.0f) / color4a.height, 0.0f, 0.0f));
61 RenderTexture color4b = RenderTexture.GetTemporary (rtW/4, rtH/4, 0);
62 Graphics.Blit (color4a, color4b, separableBlurMaterial);
63 RenderTexture.ReleaseTemporary (color4a);
64
65 separableBlurMaterial.SetVector ("offsets", new Vector4 ((blurSpread * 1.0f) / color4a.width, 0.0f, 0.0f, 0.0f));
66 color4a = RenderTexture.GetTemporary (rtW/4, rtH/4, 0);
67 Graphics.Blit (color4b, color4a, separableBlurMaterial);
68 RenderTexture.ReleaseTemporary (color4b);
69
70 // composite
71
72 contrastCompositeMaterial.SetTexture ("_MainTexBlurred", color4a);
73 contrastCompositeMaterial.SetFloat ("intensity", intensity);
74 contrastCompositeMaterial.SetFloat ("threshold", threshold);
75 Graphics.Blit (source, destination, contrastCompositeMaterial);
76
77 RenderTexture.ReleaseTemporary (color4a);
78 }
79 }
80 }